package com.library_manage_system.strategy;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.library_manage_system.view.WindowsMainImpl;
import com.library_manager_system.Book;
import com.library_manager_system.utils.GetBookByName;
import com.library_manager_system.utils.LibraryDao;

public class LoggingPanelStrategy implements PanelStrategy{

	private JLabel jl1, jl2, jl3, jl4, jl5, jl6, jl7, jl8, jl9, jl10;
	private JTextField jt1, jt2, jt3, jt4, jt5, jt6, jt7, jt8, jt9, jt10;	
	
	@Override
	public JPanel createPanel(final ArrayList<Book> arraylist,final WindowsMainImpl windowsMainImpl){
		// TODO Auto-generated method stub

		JPanel bookDataLoggingPanel = new JPanel();
		bookDataLoggingPanel.setLayout(new BorderLayout());

		JPanel loggingData = new JPanel();
		// 给这个面板设置网格布局，用于填写书籍的信息并录入
		loggingData.setLayout(new GridLayout(5, 4, 5, 5));

		// 给jlabel进行初始化
		jl1 = new JLabel("主编");
		jl2 = new JLabel("出版社");
		jl3 = new JLabel("出版年");
		jl4 = new JLabel("版次");
		jl5 = new JLabel("ISBN");
		jl6 = new JLabel("书名");
		jl7 = new JLabel("定价");
		jl8 = new JLabel("拥有本数");
		jl9 = new JLabel("借出本数");
		jl10 = new JLabel("现余本数");

		// 给文本框进行初始化
		jt1 = new JTextField(10);// 主编
		jt2 = new JTextField(10);// 出版社
		jt3 = new JTextField(10);// 出版年
		jt4 = new JTextField(10);// 版次
		jt5 = new JTextField(10);// ISBN
		jt6 = new JTextField(10);// 书名
		jt7 = new JTextField(10);// 定价
		jt8 = new JTextField(10);// 拥有本数
		jt9 = new JTextField(10);// 借出本数
		jt10 = new JTextField(10);// 现余本数

		// 对jt4,jt5,jt7,jt8,jt9,jt10添加监听，使他们只能输入数字
		jt4.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				int keyChar = e.getKeyChar();
				if (keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9) {

				} else {
					e.consume(); // 关键，屏蔽掉非法输入
				}
			}
		});
		jt5.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				int keyChar = e.getKeyChar();
				if (keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9) {

				} else {
					e.consume(); // 关键，屏蔽掉非法输入
				}
			}
		});
		jt6.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub
				Book bookByName = GetBookByName.getBookByName(arraylist, jt6
						.getText().trim());
				if (bookByName != null) {

					JOptionPane.showMessageDialog(null,
							"您录入的书已经在图书馆了，请更换书名重新录入！", "图书馆管理系统",
							JOptionPane.ERROR_MESSAGE);
					jt6.setText("");
				}
			}

			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub

			}
		});

		jt7.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				int keyChar = e.getKeyChar();
				if (keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9) {

				} else {
					e.consume(); // 关键，屏蔽掉非法输入
				}
			}
		});
		jt8.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				int keyChar = e.getKeyChar();
				if (keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9) {

				} else {
					e.consume(); // 关键，屏蔽掉非法输入
				}
			}
		});
		jt9.setText("0");
		jt9.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				int keyChar = e.getKeyChar();
				if (keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9) {

				} else {
					e.consume(); // 关键，屏蔽掉非法输入
				}

			}
		});
		jt9.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub
				if (Integer.parseInt(jt9.getText().trim()) != 0) {

					JOptionPane.showMessageDialog(null, "借出总数只能为0！", "图书馆管理系统",
							JOptionPane.ERROR_MESSAGE);
					jt9.setText("");
				}
			}

			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub

			}
		});
		jt10.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub
				if ((!(jt8.getText().equals("")))
						&& (!(jt9.getText().equals("")))) {
					int t = (Integer.parseInt(jt8.getText()))
							- (Integer.parseInt(jt9.getText()));
					String t1 = Integer.toString(t);
					if (t <= 0) {

					} else {
						jt10.setText(t1);
					}
				}
			}
		});

		// 添加到logginPanel中
		loggingData.add(jl1);
		loggingData.add(jt1);

		loggingData.add(jl2);
		loggingData.add(jt2);

		loggingData.add(jl3);
		loggingData.add(jt3);

		loggingData.add(jl4);
		loggingData.add(jt4);

		loggingData.add(jl5);
		loggingData.add(jt5);

		loggingData.add(jl6);
		loggingData.add(jt6);

		loggingData.add(jl7);
		loggingData.add(jt7);

		loggingData.add(jl8);
		loggingData.add(jt8);

		loggingData.add(jl9);
		loggingData.add(jt9);

		loggingData.add(jl10);
		loggingData.add(jt10);

		JLabel jlabel = new JLabel("请在下面填写书籍的信息");
		JPanel jpanel = new JPanel();
		jpanel.add(jlabel);

		JPanel jpanel2 = new JPanel();
		jpanel2.add(loggingData);
		bookDataLoggingPanel.add(jpanel, BorderLayout.NORTH);
		bookDataLoggingPanel.add(jpanel2, BorderLayout.CENTER);

		JPanel login = new JPanel();
		JButton summit = new JButton("提交");
		summit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

				// 新建一个书对象，并把对象加到arraylist中
				if ((jt1.getText().trim().equals(""))
						|| (jt2.getText().trim().equals(""))
						|| (jt3.getText().trim().equals(""))
						|| (jt4.getText().trim().equals(""))
						|| (jt5.getText().trim().equals(""))
						|| (jt6.getText().trim().equals(""))
						|| (jt7.getText().trim().equals(""))
						|| (jt8.getText().trim().equals(""))
						|| (jt9.getText().trim().equals(""))
						|| (jt10.getText().trim().equals(""))) {

					JOptionPane.showMessageDialog(null, "录入失败，请重新录入",
							"图书馆管理系统", JOptionPane.ERROR_MESSAGE);

				} else {
					Book book = new Book(jt1.getText(), jt2.getText(), jt6
							.getText(), jt3.getText(), Integer.parseInt(jt4
							.getText().trim()), Integer.parseInt(jt5.getText()
							.trim()), Integer.parseInt(jt7.getText().trim()),
							Integer.parseInt(jt8.getText().trim()), Integer
									.parseInt(jt9.getText().trim()), Integer
									.parseInt(jt10.getText().trim()));
					arraylist.add(book);
					// 把数据写到文档中去
					LibraryDao.writeBook(book, 1);
					show(arraylist);
					System.out.println("-------------------");
					//WindowsMainImpl.init(arraylist);
					windowsMainImpl.bookListChange(arraylist);
					
					JOptionPane.showMessageDialog(null, "录入成功");
					init();
				}
			}
		});
		login.add(summit);

		bookDataLoggingPanel.add(login, BorderLayout.SOUTH);

		return bookDataLoggingPanel;

	}

	public void show(ArrayList<Book> arraylist) {

		Iterator<Book> it = arraylist.listIterator();
		while (it.hasNext()) {
			Book book = it.next();
			book.showAll();
		}
	}

	public void init() {
		jt1.setText("");
		jt2.setText("");
		jt3.setText("");
		jt4.setText("");
		jt5.setText("");

		jt6.setText("");
		jt7.setText("");
		jt8.setText("");
		jt9.setText("0");
		jt10.setText("");

	}
	

}
